define(function(require, exports, module) {
  var $ = require("$");
  var bootbox = require("bootbox");
  var dtUtil = require('datatableutil');
  var loader = require("./loader/loader");
  require("bootstrap_commonrequire");
  require("moment");
  require('daterangepicker.css');
  require('bootstrap-daterangepicker');
  require('select2.css');
  require('select2');

  exports.init = function(param) {

    $(document).ready(function() {
      
      loader.loading("数据加载中……");

      // 加载自定义模板-条件区域
      var template = require("./accountinfo.tpl");
      var Widget = require("widget");
      var Component = Widget.extend({
        template : template,
      });
      new Component({
        renderTo : "queryAreaId"
      });
      function getCondition() {
        //获取查询条件
        var isvName = $('#isvName').val();
        var queryStr = "";
        //设置此页面只查询应用账号
        if (isvName != '' && isvName != null) {
          queryStr += ("isvName=" + isvName + ";");
        }
        //获取app查询信息
        var app_name = $('#app_id_select').val();
        if (app_name != '' && app_name != null) {
          queryStr += ("app_name=" + app_name + ";");
        }
      //获取app查询信息
        var app_key = $('#app_key_input').val();
        if (app_key != '' && app_key != null) {
          queryStr += ("app_key=" + app_key + ";");
        }
        return queryStr;
      }
      //加载表单模板和数据
      var Table = require("table");
      // 查询内容显示
      new Table({
        attrs : {
          id : "data_table",
          isHovered : false,
          isStriped : true,
          isBordered : false
        },
        renderTo : param.divWrapperId

      });
      // isv信息显示
      var isvinfo_data_table = $('#data_table').dataTable({
        "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "bJQueryUI" : false,
        "oLanguage" : {
          "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
        },
        "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
        'bPaginate' : true, // 是否分页
        "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
        "bServerSide" : true, // 指定服务器端分页
        "bDeferRender" : true,
        "bFilter" : true,
        "bSort" : true,
        "bInfo" : true,
        "sAjaxSource" : param.ajaxUrl,
        "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.MIN, // 默认分页的每页记录数
        "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
          var queryparam = dtUtil.convert_aoData(aoData);

          //拼接where查询条件
          var cond = getCondition();
          if (cond != '' && cond != null) {
            queryparam.q += cond;
          }
          // 添加自己的额外参数
          oSettings.jqXHR = $.ajax({
            "dataType" : 'json',
            "contentType" : "application/json;charset=UTF-8",
            "type" : "GET",
            "url" : sSource,
            "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
            "success" : function(json) {
              loader.noloading();
              if (json.flag == '000000') {
                dtUtil.successCallback(json, aoData, fnCallback);
              } else {
                oauth2Relogin();
              }
            }
          });

        },
        "aoColumns" : [ {
          "sTitle" : "id",
          "bSortable" : false,
          "mData" : "app_id",
          "bVisible" : false
        }, {
          "sTitle" : "ISV",
          "bSortable" : false,
          "mData" : "isv_name"
        },{
          "sTitle" : "应用",
          "bSortable" : false,
          "mData" : "app_name"
        },{
          "sTitle" : "应用标识",
          "bSortable" : false,
          "mData" : "app_key"
        },{
            "sTitle" : "外系统编码",
            "bSortable" : false,
            "mData" : "periphery_code"
          }, {
          "sTitle" : "扩展信息",
          "bSortable" : false,
          "mData" : "isHasExtend",
          "mRender" : function(data, type, full) {
            if (data == true)
              return "已配置";
            else if (data == false)
              return "未配置";
          }
        },
        {
          "sTitle" : "操作",
          "bSortable" : false,
          "mData" : "periphery_code",
          "mRender" : function(data, status, full) {
            var ret = '<style>.yalei a{ float:left;}</style><div class="btn-group yalei" style="margin-top: 8px;">';
            ret += '<a href="javascript:void(0);" class="btn icon-reply contrast" title="修改"></a>';
//            需要修改下按钮位置处的图标
            ret += '<a href="javascript:void(0);" class="btn icon-edit-sign contrast" title="编辑计费规则"></a>';
            
            ret += '<div class="navbar" style="float:left;margin-top: -5px;"><ul class="nav pull-right">'
              +'<li class="dropdown light only-icon">'
              +'<a class="dropdown-toggle btn icon-cog contrast" data-toggle="dropdown" href="javascript:void(0);" style="line-height: 3px;"></a>'
              +'<ul class="dropdown-menu">'
              +'<li><a href="javascript:void(0);" class="btn icon-wrench"><span>&nbsp;账号号码配置</span></a></li>'
              +'<li><a href="javascript:void(0);" class="btn icon-envelope"><span id="emailNoticeSpan">&nbsp;发送邮件通知</span></a></li>'
              +'<li><a href="javascript:void(0);" class="btn icon-refresh"><span id="switchNumSpan">&nbsp;主动切换号码</span></a></li>'
              +'</ul></li></ul></div>';
            
            return ret += '</div>';
          }
        } ],
        "fnDrawCallback" : function(oSettings) {
          // 表格绘制完成后，去除width属性，使表格可缩放
          $('#' + oSettings.sTableId).css("width", "");
        }
      });
      //修改按钮
      $("#data_table tbody").on("click", "tr>td>div>a.icon-reply", function(e) {
//        修改一下上面的按钮图标
        $('#accountinfo_data_table').hide();
        $('#accountinfo_data_table2').hide();
        $("#accountForm").hide();
        $("#queryAreaId").hide();
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = isvinfo_data_table.fnGetData(rowNode);
        var isvinforeturnform = require("./accountinforeturnform");
        isvinforeturnform.init("accountinfo_data_table_edit", rowData);
        $('#accountinfo_data_table_edit').show();
      });
      //编辑计费规则按钮
      $("#data_table tbody").on("click", "tr>td>div>a.icon-edit-sign", function(e) {
        $('#accountinfo_data_table').hide();
        $("#accountForm").hide();
        $("#queryAreaId").hide();
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = isvinfo_data_table.fnGetData(rowNode);
        var isvinforeturnform = require("./ruleaccountinforeturnform");
        isvinforeturnform.init("accountinfo_data_table_edit", rowData);
        $('#accountinfo_data_table_edit').show();
        $('#accountinfo_data_table2').show();
        
      });
      /*
      //编辑能力号码关系按钮
      $("#data_table tbody").on("click", "tr>td>div>a.icon-eraser", function(e) {
        $('#accountinfo_data_table').hide();
        $("#accountForm").hide();
        $("#queryAreaId").hide();
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = isvinfo_data_table.fnGetData(rowNode);
        var isvinforeturnform = require("./accountinfoformnumaccrel");
        isvinforeturnform.init("accountinfo_data_table_edit", rowData);
        $('#accountinfo_data_table_edit').show();
        $('#accountinfo_data_table2').show();
      });
        */
      // 账号号码配置按钮-下拉菜单
      $("#data_table tbody").on("click", "tr>td>div>div>ul>li>ul>li>a.icon-wrench", function(e) {
        $(".dropdown-toggle").dropdown('toggle');
        
        $('#accountinfo_data_table').hide();
        $("#accountForm").hide();
        $("#queryAreaId").hide();
        var rowNode = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
        var rowData = isvinfo_data_table.fnGetData(rowNode);
        var isvinforeturnform = require("./accountinfopoolaccrel");
        isvinforeturnform.init("accountinfo_data_table_edit", rowData);
        $('#accountinfo_data_table_edit').show();
        $('#accountinfo_data_table2').show();
        
        return false;
      });
      
      
      // 邮件通知按钮事件-下拉菜单
      $("#data_table tbody").on("click", "tr>td>div>div>ul>li>ul>li>a.icon-envelope", function(e) {
        $(".dropdown-toggle").dropdown('toggle');
        var rowNode = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
        var rowData = isvinfo_data_table.fnGetData(rowNode);
        var accountId = rowData.app_key;
        var type = "emailNotice";
        
        myDetail(accountId,type);
      });
      
      // 主动切换号码按钮事件-下拉菜单
      $("#data_table tbody").on("click", "tr>td>div>div>ul>li>ul>li>a.icon-refresh", function(e) {
        $(".dropdown-toggle").dropdown('toggle');
        var rowNode = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
        var rowData = isvinfo_data_table.fnGetData(rowNode);
        var accountId = rowData.app_key;
        var type = "switchNum";
        
        myDetail(accountId,type);
      });
      
      // 查看详情
      var myDetail = function(context,type) {
        var my_detail = require("./accountcompanypoolaccrelmodal");
        if ($("#mydetail_id").length <= 0)
          $("body").append("<div id='mydetail_id'></div>");
        my_detail.init({
          "title" : "提示",
          "renderTo" : "mydetail_id",
          "details": context,
          "type": type
        });
      }
      
      
      //查询按钮
      $('#querybtn').click(function() {
        loader.loading("数据加载中……");
        isvinfo_data_table.fnFilter("");
        return false;
      });

      //重置按钮
      $('#restbtn').click(function() {
        loader.loading("数据加载中……");
        
        $('#app_id_select').val("");
        $("#isvName").val("");
        $("#app_key_input").val("");
        isvinfo_data_table.fnFilter("");
        return false;
      });

    });

  };
});
